import { _decorator, Component, Node, Camera, Label, Vec2, v3, tween, Tween } from 'cc';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import GlobalData from '../../Init/Config/GlobalData';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;
/**其他角色对话 */
@ccclass('OtherDialogCmp')
export class OtherDialogCmp extends Component {
    guideIdRec: { [id: number]: number } = {};
    recs: { [uuid: string]: DialogRec } = {};

    onLoad() {
        EventManager.on(EventTypes.UIEvents.ShowDialog, this.onShowDialog, this);
    }

    onEnable() {
        this.reset();
    }

    reset() {
        for (const key in this.recs) {
            if (this.recs[key]) {
                this.recs[key].reset();
            }
        }
        this.recs = {};
        this.guideIdRec = {};
    }

    update(dt) {
        for (const key in this.recs) {
            const e = this.recs[key];
            if (e && e.isShowing) {
                e.update(dt);
            } else {
                e.reset();
                delete this.recs[key];
            }
        }
    }

    onShowDialog(d: { t: GlobalEnum.DialogTargetType, target: Node, isGuide?: boolean, offsetY?: number, dialogId?: number }) {
        if (undefined == d.isGuide) { d.isGuide = false };
        if (undefined == d.offsetY) { d.offsetY = 0 };
        //引导对话只出现一次
        if (d.isGuide && this.guideIdRec[d.dialogId]) {
            return;
        } else {
            //记录
            if (undefined !== d.dialogId) {
                this.guideIdRec[d.dialogId] = 1;
            }
        }

        const cfg = d.isGuide ? OtherGuideCfg : OtherDialogCfg;
        let arr = cfg[d.t];
        if (!arr || arr.length <= 0) return;
        let msg;

        if (undefined == d.dialogId) {
            msg = arr[Math.floor(Math.random() * arr.length)];
        } else {
            msg = arr[d.dialogId];
            if (!msg) {
                console.warn('对话ID错误:', d.dialogId);
            }
        }

        let e = GlobalPool.get('dialog');
        e.parent = this.node;
        let cmp = new DialogRec(e, d.target, d.offsetY, msg);
        this.recs[e.uuid] = cmp;
    }
}


export class DialogRec {
    node: Node;
    target2d: Node;
    camera: Camera;
    pos = v3();
    animScale = v3();
    isShowing = false;
    delay = 3;
    pos3D = v3();
    offSetY = 0;
    curt = 0;
    constructor(node: Node, target2d: Node, offSetY: number, str: string) {
        this.offSetY = offSetY;
        this.camera = GlobalData.get(GlobalEnum.GlobalDataType.Camera3D);
        this.node = node;
        this.target2d = target2d;
        let label = node.getComponentInChildren(Label);
        label.string = str;
        this.animScale.set(0, 0, 0);
        this.node.setScale(this.animScale);
        this.curt = 0;
        this.showDialog();
        this.calProps();
    }

    update(dt) {
        if (this.isShowing) {
            //位置
            this.calProps();
            this.curt += dt;
            if (this.curt >= this.delay) {
                this.isShowing = false;
                this.hideDialog();
            }
        }
    }

    tmpPos = v3();
    calProps() {
        if (!this.node.parent) {
            this.curt = this.delay; //提前结束
            return;
        };

        if (this.target2d && this.target2d.active) {
            this.pos3D.x = this.target2d.position.x * GlobalConfig.Scale2DTo3D;
            this.pos3D.z = -this.target2d.position.y * GlobalConfig.Scale2DTo3D;
            this.pos3D.y = this.offSetY;
        } else {
            this.curt = this.delay; //提前结束
        }
        this.camera.convertToUINode(this.pos3D, this.node.parent, this.tmpPos);
        this.node.setPosition(this.tmpPos);
        this.node.setScale(this.animScale);
    }

    //显示
    showDialog() {
        this.isShowing = true;
        //动画
        this.animScale.set(0, 0, 0);
        tween(this.animScale).
            to(0.5, { x: 1, y: 1 }, { easing: 'backOut' }).
            start();
    }

    //隐藏
    hideDialog() {
        tween(this.animScale).
            to(0.5, { x: 0, y: 0 }, { easing: 'backIn' }).
            call(() => {
                this.isShowing = false;
            }).start();
    }

    reset() {
        GlobalPool.put(this.node);
        Tween.stopAllByTarget(this.animScale);
    }
}

//其他角色的对话-引导固定
export const OtherGuideCfg = {
    //people
    0: [
        '哇！有僵尸啊！',
        '你好漂亮呀？',
    ],
    //Army
    1: [
        '火力真猛!',
    ],
    //TankBoss
    2: [
        '冲啊,干翻这个大家伙!',
    ],
    //ArmyBoss
    3: [
        '好大的脑子啊!',
    ],
    //地雷
    4: [],

    //tips
    5: []
}

//其他角色的对话-随机
export const OtherDialogCfg = {
    //people
    0: [
        '哇！有僵尸啊！',
        '好可怕啊!'
    ],
    //Army
    1: [
        '火力真猛!',
    ],
    //TankBoss
    2: [
        '冲啊,干翻这个大家伙!',
    ],
    //ArmyBoss
    3: [
        '好大的脑子啊!',
    ],
    //地雷
    4: [],

    //tips
    5: []
}